package com.cgbl.cg.vm.mine

import android.app.Application
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField
import androidx.databinding.ObservableInt
import api.UserServiceFactory
import api.WorkServiceFactory
import com.blankj.utilcode.util.LogUtils
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.configs.PrefsManager
import com.robot.base.model.CollectDetailBean
import com.robot.base.model.PageListBean
import com.robot.base.model.UserInfoBean
import com.robot.base.model.WorksBean
import com.robot.base.util.arouter.ARouterUtil
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse
import me.goldze.mvvmhabit.utils.StringUtils

class OtherPersonalPageViewModel(application: Application) : GLBaseViewModel(application) {

    val uc = UiChangeEvent()


    val tabType = ObservableInt(TAB_WORKS)

    val clickSelectWorks = BindingCommand<Any>(BindingAction {
        tabType.set(TAB_WORKS)
        uc.switchTabEvent.postValue(true)
    })

    val clickSelectCollection = BindingCommand<Any>(BindingAction {
        tabType.set(TAB_COLLECTION)
        uc.switchTabEvent.postValue(false)
    })

    val userAvatar = ObservableField<String>()
    val nickNameField = ObservableField<String>()
    val engineerStrField = ObservableField<String>()
    val hasEngineerField = ObservableBoolean()
    val fansCountField = ObservableField<String>()
    val followCountField = ObservableField<String>()
    val overallIntegralField = ObservableField<String>()

    val releaseCountField = ObservableField<String>()

    var page = 1

    val pageSize = 10

    var collectionPage = 1

    var collectionPageSize = 10

    var uid = ""

    val follow = ObservableField(false)
    val followText = ObservableField<String>("+关注")

    val followCommand = BindingCommand<Any>(BindingAction {
        followUsers()
    })

    fun followUsers() {
        UserServiceFactory.followUsers(
            mapOf(
                "followerAccountId" to uid
            )
        ).subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
            override fun onNext(t: BaseResponse<String>) {
                LogUtils.e(t)
                follow.set(!(follow.get() ?: false))
                followText.set(
                    if (follow.get() == true) {
                        "已关注"
                    } else {
                        "+关注"
                    }
                )
            }
        })
    }

    fun fetchUidByUserIndo(uid: String) {
        this.uid = uid
        UserServiceFactory.otherUserInfo(uid)
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<UserInfoBean>>(this) {
                override fun onNext(t: BaseResponse<UserInfoBean>) {

                    t.data.apply {
                        nickNameField.set(nickName)
                        engineerStrField.set(engineer)
                        hasEngineerField.set(!StringUtils.isEmpty(engineer))
                        fansCountField.set(fansCount)
                        followCountField.set(followCount)
                        overallIntegralField.set(overallIntegral)
                        userAvatar.set(head)
                        this@OtherPersonalPageViewModel.follow.set(follow)
                        followText.set(
                            if (follow == true) {
                                "已关注"
                            } else {
                                "关注"
                            }
                        )
                    }

                    refresh()
                    collectPage()
                }
            })
    }

    fun refresh() {
        if (tabType.get() == TAB_WORKS) {
            page = 1
            fetchWork()
            return
        }
        collectionPage = 1
        collectPage()
    }

    fun loadMore() {
        if (tabType.get() == TAB_WORKS) {
            page++
            fetchWork()
            return
        }
        collectionPageSize++
        collectPage()
    }

    fun fetchWork() {
        WorkServiceFactory.page(
            mapOf(
                "pageNo" to page, "pageSize" to pageSize, "isAccount" to "0","accountId" to uid
            )
        ).subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<PageListBean<WorksBean>>>(this) {
            override fun onNext(t: BaseResponse<PageListBean<WorksBean>>) {
                releaseCountField.set("共发布${t.data.total}组作品")
                if (page == 1) {
                    uc.newWorksData.postValue(t.data.list)
                } else {
                    uc.loadMoreWorksData.postValue(t.data.list)
                }
            }

            override fun onError(e: Throwable) {
                super.onError(e)
                uc.newWorksData.postValue(listOf())
            }
        })
    }

    fun collectPage() {
        WorkServiceFactory.collectPage(
            mapOf(
                "pageNo" to collectionPage, "pageSize" to collectionPageSize, "isAccount" to "1","isAccount" to "0","accountId" to uid
            )
        ).subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<PageListBean<CollectDetailBean>>>(this) {
            override fun onNext(t: BaseResponse<PageListBean<CollectDetailBean>>) {
                if (page == 1) {
                    uc.newCollectionPageData.postValue(t.data.list)
                } else {
                    uc.loadMoreCollectionPageData.postValue(t.data.list)
                }
            }

            override fun onError(e: Throwable) {
                super.onError(e)
                uc.newCollectionPageData.postValue(listOf())
            }
        })
    }

    class UiChangeEvent {

        val newWorksData: SingleLiveEvent<List<WorksBean>> = SingleLiveEvent()

        val loadMoreWorksData: SingleLiveEvent<List<WorksBean>> = SingleLiveEvent()

        val newCollectionPageData: SingleLiveEvent<List<CollectDetailBean>> = SingleLiveEvent()

        val loadMoreCollectionPageData: SingleLiveEvent<List<CollectDetailBean>> = SingleLiveEvent()

        val switchTabEvent: SingleLiveEvent<Boolean> = SingleLiveEvent()
    }


    companion object {

        const val TAB_WORKS = 1

        const val TAB_COLLECTION = 2

    }

}